package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.*;
import com.example.springboot.service.AdminService;
import com.example.springboot.service.CheckExceptionService;
import com.example.springboot.service.CheckRoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/checkroom")
public class CheckRoomController {

    @Resource
    private CheckRoomService checkRoomService;
    @Resource
    private CheckExceptionService checkExceptionService;

    /**
     * 添加学生查寝信息
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody CheckRoom checkRoom) {
        int i = checkRoomService.addcheckRoom(checkRoom);
//        判断是否归寝异常
        if (checkRoom.getYdpeople() - checkRoom.getSdpeople() != 0) {
            CheckException checkException = new CheckException();
            checkException.setDormbuildid(checkRoom.getDormbuildid());
            checkException.setDormroomid(checkRoom.getDormroomid());
            checkException.setStatus(2);
            checkExceptionService.addCheckException(checkException);
        }
        if (i == -2) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }


    /**
     * 查找归寝记录
     */
    @GetMapping("/find")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam String search) {
//        int count= checkRoomService.findcount(pageNum, pageSize, search);
        PageInfo list = checkRoomService.find(pageNum, pageSize, search);
        if (list != null) {
            return Result.success(list);
//            return Result.success(list);

        } else {
            return Result.error("-1", "查询失败");
        }
    }


    /**
     * 楼宇信息更新
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody CheckRoom checkRoom) {
        int i = checkRoomService.updateCheckRoom(checkRoom);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.error("-1", "更新失败");
        }
    }


    /**
     * 楼宇删除
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam(defaultValue = "0") Integer id) {
        int i = checkRoomService.deleteCheckRoom(id);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }

}
